/**
 * 获取店铺评论数据
 */

const puppeteer = require('puppeteer')
const path = require('path')
const fs = require('fs') 
const { fork } = require('child_process')
const readFile = require('./readFile')
var numCPUs = require('os').cpus().length
const protocol = require('./MessageActions');

//需要抓取的城市数据
const need_fetch_cities = [ { name:'chengdu',url: 'http://www.dianping.com/shoplist/search/8_10_0_score '},{
  name: 'beijing',url: 'http://www.dianping.com/shoplist/search/2_10_0_score'
},  { name: 'guangzhou' ,url: 'http://www.dianping.com/shoplist/search/4_10_0_score'},
{ name: 'shenzhen', url: 'http://www.dianping.com/shoplist/search/7_10_0_score'},]


let shops = readFile(need_fetch_cities)

var workers = {};

if(shops && shops.length > 0){
  forkWorkers(shops)
}




function forkWorkers( shops ){


  console.log(`需要抓取的店铺数量:${ shops.length  }`)

  console.log(`CPU length: ${ numCPUs }`)

  if (shops && shops.length > 0) {
    if (shops.length == 1) {
      numCPUs = 1
    }

    //平均每个进程需要处理的数量
    let avg_worker_num =  shops.length / numCPUs

    console.log(`平均每个进程需要处理的数量: ${avg_worker_num} `)

    for (let index = 0; index < numCPUs; index++) {

      let tasks = [];

      if (index == numCPUs - 1 && shops.length % numCPUs !== 0) {

        tasks = shops.slice( (index * avg_worker_num ), (index * avg_worker_num ) + avg_worker_num + shops.length % numCPUs )

      }else{

        if (index == 0  ) {

          tasks = shops.slice(0, avg_worker_num ) ;

        }
        else{
          tasks =  shops.slice( (index * avg_worker_num ), (index * avg_worker_num ) + avg_worker_num )    
        } 
      } 

      console.log(`${index} ${tasks.length}  `)
      const worker = fork('worker.js');

      worker.send(tasks);

      worker.on('message', (msg) => {
        console.info(` worker: ${ worker.pid} receive msg from master: `)
      });

      worker.on('exit', (code)=> {
        console.log(`worker ${worker.pid} died with code: ${code}`);

        delete workers[worker.pid];
      });

      worker.on('uncaughtException', (err) => {
        console.log(`${worker.pid}  uncaughtException with err: ${err}`);

        delete workers[worker.pid];
        
      });

      workers[worker.pid] = { worker, tasks };
    }  

  }

}

process.on('message', (msg) => {
  console.info(`receive message from worker process:${ msg.worker_pid}, type: ${ msg.type}  `)

  if (msg.type === protocol.CHANGE_PROXY ){



    delete workers[msg.worker_pid];

    console.info(`change proxy!!!!! ` );

    const worker = fork('worker.js');

    worker.send(msg.data);

    worker.on('message', (msg) => {
      console.info(` worker: ${ worker.pid} receive msg from master:  ${ JSON.stringify(msg)}  `)
    });

    worker.on('exit', (code)=> {
      console.log(`worker ${worker.pid} died with code: ${code}`);

      delete workers[worker.pid];
    });

    worker.on('uncaughtException', (err) => {
      console.log(`${worker.pid}  uncaughtException with err: ${err}`);

      delete workers[worker.pid];

    });

    workers[worker.pid] = { worker,tasks: msg.data };
  }
})






